package com.example.duckshop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.duckshop.R;
import com.example.duckshop.bean.OrdersVo;
import com.bumptech.glide.request.RequestOptions;

import java.util.ArrayList;
import java.util.List;
//订单适配器
//适配器目的就是将数据与控件进行适配调控达到我们想要的结果，只需要将数据传输到自定义的适配器里就可以达到理想的效果
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {
    private List<OrdersVo> list =new ArrayList<>();
    private Context mActivity;
    private RequestOptions headerRO = new RequestOptions().circleCrop();//圆角变换
    private ItemListener mItemListener;
    public void setItemListener(ItemListener itemListener){
        this.mItemListener = itemListener;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mActivity = viewGroup.getContext();
        View view= LayoutInflater.from(mActivity).inflate(R.layout.item_rv_order_list,viewGroup,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        OrdersVo order = list.get(i);
        if (order !=null) {
            viewHolder.nickName.setText(String.format("用户:%s",order.getNickName()));
            viewHolder.j.setText(String.format("￥%s",order.getAmount()));//order.getNumber
            viewHolder.date.setText(order.getDate());
            viewHolder.title.setText(order.getTitle());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemListener !=null) {
                        mItemListener.ItemClick(order);
                    }
                }
            });
            viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    if (mItemListener!=null){
                        mItemListener.Delete(order);
                    }

                    return false;
                }
            });
        }
    }
    public void addItem(List<OrdersVo> listAdd) {
        //如果是加载第一页，需要先清空数据列表
        this.list.clear();
        if (listAdd!=null){
            //添加数据
            this.list.addAll(listAdd);
        }
        //通知RecyclerView进行改变--整体
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView nickName;
        private TextView date;
        private TextView j;
        private TextView title;
      //  private ImageView img;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            nickName = itemView.findViewById(R.id.nickName);
            date = itemView.findViewById(R.id.date);
            title = itemView.findViewById(R.id.title);
            j=itemView.findViewById(R.id.j);
           // img=itemView.findViewById(R.id.img);
        }
    }
    public interface ItemListener{
        void ItemClick(OrdersVo order);
        void Delete(OrdersVo order);
    }
}
